package com.example.childpositionapp.network;



import android.annotation.SuppressLint;
import android.util.Log;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class Util {
    private static String URL="http://175.178.213.138:8081/cjntsb/user";
    private static String token;
    public static void postHttp(String url,String text, okhttp3.Callback callback){
        OkHttpClient client=new OkHttpClient();
        MediaType JSON = MediaType.Companion.parse("application/json;charset=utf-8");
        RequestBody body=RequestBody.Companion.create(text,JSON);
        Request request=new Request.Builder()
                .url(URL+url)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void getHttp(String url, okhttp3.Callback callback){
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder()
                .url(URL+url)
//                .addHeader("Authorization",token)
                .addHeader("token",token)
                .build();
        client.newCall(request).enqueue(callback);
    }
    public static void getYzm(String url, okhttp3.Callback callback){
        OkHttpClient client=new OkHttpClient();
        Log.d("3333333333333",URL+url);
        Request request=new Request.Builder()
                .url(URL+url)
//                .addHeader("Authorization",token)
//                .addHeader("token",token)
                .get()
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void setToken(String token) {
        Util.token = token;
    }

    public static String getToken() {
        return Util.token;
    }

    @SuppressLint("LongLogTag")
    public static void postToken(String url, String text, okhttp3.Callback callback){
        OkHttpClient client=new OkHttpClient();
        MediaType JSON = MediaType.Companion.parse("application/json;charset=utf-8");
        RequestBody body=RequestBody.Companion.create(text,JSON);
        Request request=new Request.Builder()
                .url(URL+url)
                .post(body)
                .addHeader("token",token)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void putHttp(String url, okhttp3.Callback callback){
        OkHttpClient client=new OkHttpClient();
        MediaType JSON = MediaType.Companion.parse("application/json;charset=utf-8");
        RequestBody body=RequestBody.Companion.create("",JSON);
        Request request=new Request.Builder()
                .url(URL+url)
                .put(body)
                .addHeader("token",token)
                .build();
        client.newCall(request).enqueue(callback);
    }

}
